<?php

namespace App\Admin\Service;

use App\Admin\Grid\NGrid;
use App\Library\Models\AdminAuctionModel;
use App\Library\Models\AdminShopOrderModel;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Show;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

class ShopOrderService extends BaseService
{
    public string $table = 'shop_order';

    /**
     * grid
     * @return void
     */
    public function grid(): void
    {
        $this->grid = new NGrid(new AdminShopOrderModel);
        $this->grid->model()->with('goods')->leftJoin('shop_order_ext', 'shop_order_ext.order_id', 'shop_order.order_id')
            ->leftJoin('shop_user as user', 'user.user_id', 'shop_order_ext.user_id')
            ->leftJoin('shop_user as seller', 'seller.user_id', 'shop_order_ext.seller_id')
            ->where('buy_user_id', '>', 0)
            ->where(function ($query) {
                if (!empty(Admin::user()->admin_type)) {
                    $mainId = Admin::user()->user_main_id;
                    $userIdArr[] = Admin::user()->user_id;
                    if (!empty($mainId)) {
                        $userIdArr = DB::table('shop_user')
                            ->where('user_main_id', $mainId)->pluck('user_id')->toArray();
                        $userIdArr[] = $mainId;
                    }
                    $query->whereIn('user.user_id', $userIdArr);
                }
            })
        ->select($this->columns())->orderByDesc('shop_order.order_id');
        $this->filter();
    }

    /**
     * filter
     * @return void
     */
    public function filter(): void
    {
        $this->grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->equal('shop_order-order_id', 'ID')->integer();
                $filter->equal('user-user_mobile', '买家')->mobile();
                $filter->where(function (Builder $builder) {
                    if ((int)$this->input === 1) $builder->whereNotNull('system_at');
                    else $builder->whereNull('system_at');
                }, __('shop_order.systemAt'))->select(__('l.whether'));
                $filter->between('created_at', __('l.createdAt'))->datetime();
            });
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->equal('serial_number', __('l.serialNumber'));
                $filter->equal('seller-user_mobile', '卖家')->mobile();
                //$filter->equal('order_is_self_pickup', '自提')->select(__('shop_order.orderIsSelfPickupArr'));
                $filter->equal('status', __('l.status'))->select(__('shop_order.statusArr'));
            });
        });
    }

    /**
     * show
     * @param int $id
     * @return void
     */
    public function show(int $id): void
    {
        $this->id = $id;
        $this->show = new Show(AdminShopOrderModel::query()->with('goods')
            ->leftJoin('shop_order_ext', 'shop_order_ext.order_id', 'shop_order.order_id')
            ->leftJoin('shop_user as user', 'user.user_id', 'shop_order_ext.user_id')
            ->leftJoin('shop_user as seller', 'seller.user_id', 'shop_order_ext.seller_id')
            ->find($id, array_merge($this->columns(), [
                'order_product_price',
                'order_product_num',
                'discount_price',
                'active_discount',
                'sub_price',
                'order_note',
                'shop_order.invite_code',
                'order_pay_callback_json',
                'order_pay_api_status',
                'pay_at',
                'cancel_at',
                'refund_at',
                'complete_at',
                'settlement_at',
                'deliver_at',
                'confirm_at',
                'auto_confirm_at',
                'user_remark',
                'seller_remark',
                'system_remark',
            ])));
    }

    /**
     * fieldArr
     * @return array
     */
    public function fieldArr(): array
    {
        return [
            'orderProductPrice',
            'orderProductNum',
            'activeDiscount',
            'subPrice',
            'orderNote',
            'inviteCode',
            'payAt',
            'refundAt',
            'cancelAt',
            'completeAt',
            'settlementAt',
            'deliverAt',
            'confirmAt',
            'autoConfirmAt',
            'userRemark',
            'sellerRemark',
            'systemRemark',
            'orderContacts',
            'orderContactsMobile',
            'orderContactsAddress',
            'addAt',
            'orderPayApiStatus',
        ];
    }

    /**
     * columns
     * @return array
     */
    private function columns(): array
    {
        return [
            'shop_order.order_id',
            'serial_number',
            'status',
            'pay_price',
            'order_ensure_price',
            'order_is_self_pickup',
            'order_contacts',
            'order_contacts_mobile',
            'order_contacts_address',
            'user.user_mobile',
            'seller.user_mobile as seller_mobile',
            'shop_order.created_at',
            'shop_order_ext.deliver_at',
            'shop_order_ext.status_deliver',
            'shop_order_ext.confirm_at',
            'shop_order_ext.status_confirm',
            'shop_order_ext.status as ext_status',
            'shop_order_ext.freight',
            'order_contacts',
            'order_contacts_mobile',
            'order_contacts_address',
            'shop_order_ext.user_remark',
            'shop_order_ext.seller_remark',
            'shop_order_ext.system_at',
            'shop_order.is_cart',
            'shop_order.plug',
            'plug2',
            'coupon_discount',
            'help',
        ];
    }
}
